package com.orangelife.mobile.confirmOrder.biz;

import com.curry.android.util.DecimalUtil;
import com.orangelife.mobile.confirmOrder.entity.MerchantInfo;

import java.util.List;

/**
 * 预订单的业务逻辑
 * Created by 吴格非 on 2016/3/7.
 */
public class OrderBiz {
    /**
     * 计算总价
     */
    public static MerchantOrderListHelper updateAllTotalPrice(MerchantOrderListHelper merchant) {
        List<MerchantInfo> mMerchantInfoList = merchant.getMerchantInfoList();
        /*邮费*/
        String totalPostFee = "0";
        /*配送费*/
        String totalDispatchFee = "0";
        String singleTotalPrice = "0";
        String totaolGoods = "0";//商品总金额
        /*应付总额*/
        String totalPrice = "0";
        /*抵用券金额*/
        String totalVoucherPrice = "0";
        for (int i = 0; i < mMerchantInfoList.size(); i++) {
            MerchantInfo mi = mMerchantInfoList.get(i);
            totalPostFee = DecimalUtil.add(totalPostFee, mi.getShipCost());//邮费
            totalDispatchFee = DecimalUtil.add(totalDispatchFee, mi.getDispatchFee());//配送费
            totalVoucherPrice = DecimalUtil.add(totalVoucherPrice, mi.getVoucherValue());//抵用券
            if (Double.parseDouble(mi.getVoucherValue()) > Double.parseDouble(mi.getSingleTotalPrice())) {
                singleTotalPrice = DecimalUtil.add(totalDispatchFee, totalPostFee);
            } else {
                String dispatchFee = DecimalUtil.add(mi.getDispatchFee(), mi.getShipCost());
                String singlePrice = mi.getSingleTotalPrice();

                singleTotalPrice = DecimalUtil.subtract(DecimalUtil.add(singlePrice, dispatchFee), mi.getVoucherValue());
            }
            totaolGoods = DecimalUtil.add(totaolGoods, mi.getSingleTotalPrice());
            totalPrice = DecimalUtil.add(totalPrice, singleTotalPrice);
        }
        totalPrice = DecimalUtil.subtract(totalPrice, merchant.deductionPrice);//减去积分抵扣金额
        merchant.totalPostogeFee = totalPostFee;
        merchant.totalDisptachFee = totalDispatchFee;
        merchant.totalVoucherPrice = totalVoucherPrice;
        merchant.totalGoodsPrice = totaolGoods;
        if (totalPrice.contains("-")) {
            totalPrice = "0";
        }
        merchant.totalPrice = totalPrice;
        return merchant;
    }
}
